# @file    : app
# @time    : 2024/4/18
# @author  : yongpeng.yao
# @desc    :

from kivy import Logger
from kivy.app import App
from kivy.core.text import LabelBase
from kivy.core.window import Window

from common import get_conf, get_display_info
from ui.audio_player import MainWindow


class MainApp(App):

    def __init__(self):
        super(MainApp, self).__init__()
        self.title = "Audio control AI"
        self.icon = "image/Audio_control.png"
        primary_monitor, _ = get_display_info()
        primary_monitor_info = primary_monitor[2]
        # 获取屏幕左上角坐标
        window_width = abs(primary_monitor_info[2] - primary_monitor_info[0])
        window_height = abs(primary_monitor_info[3] - primary_monitor_info[1])
        # 设置窗口大小和位置
        conf = get_conf()
        self.width = conf.getint('size', 'width')
        self.height = conf.getint('size', 'height')
        Window.minimum_width = 1080
        Window.minimum_height = 800
        Window.size = (self.width, self.height)
        Window.left = int((window_width - self.width) / 4)
        Window.top = int((window_height - self.height) / 4)
        self.do_config()

    def build(self):
        return MainWindow()

    def open_settings(self, *args):
        return False

    @staticmethod
    def do_config():
        try:
            # 设置默认字体
            LabelBase.register(name='Roboto', fn_regular='data/fonts/MiSans-Regular.ttf',
                               fn_bold='data/fonts/MiSans-Demibold.ttf')
        except IOError:
            Logger.error("Error: unable to load font file")
